package org.hishen.example.web.admin.system;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.hishenframework.data.Page;
import org.hishen.example.entity.system.User;
import org.hishen.example.service.admin.system.UserService;

@Controller("AdminUserController")
@RequestMapping(UserController.PATH)
public class UserController {

	public static final String PATH = "/admin/system/user";
	public static final String RELOAD = "redirect:" + PATH;
	
	@Resource
	private UserService userService;
	
	/**
	 * 用户
	 * @param page 分页对象
	 * @return 路径
	 */
	@RequestMapping
	public String list(Page page) {
		userService.list(page);
		return PATH + "/list";
	}
	
	/** 添加用户 */
	@RequestMapping("/create")
	public void create() {}
	
	/**
	 * 保存用户
	 * @param user 用户实体
	 * @return
	 */
	@RequestMapping("/save")
	public String save(User user) {
		userService.save(user);
		return RELOAD;
	}
	
	/**
	 * 编辑用户
	 * @param model 模型
	 * @param id 主键
	 * @return 路径
	 */
	@RequestMapping("/edit/{id}")
	public String edit(Model model, @PathVariable Integer id) {
		model.addAttribute(userService.get(id));
		return PATH + "/edit";
	}
	
	/**
	 * 更新用户
	 * @param user 用户实体
	 * @return 路径
	 */
	@RequestMapping("/update")
	public String update(User user) {
		userService.update(user);
		return RELOAD;
	}

	/**
	 * 删除用户
	 * @param id 主键
	 * @return 路径
	 */	
	@RequestMapping("/delete")
	public String delete(Integer[] id) {
		userService.delete(id);
		return RELOAD;
	}
}
